<?php
/*
 * Created on 2009-7-20
 *
 * Author: guobin@yizin.net
 */
 
require_once ("platform/FeatureManagerUI.php");

class com_yizin_fm_RouterExtension extends Extension
{
    public function apply( & $context, Chain $chain)
    {
        $context['rule']['{^/admin/fm/.*$}'] = dirname( __FILE__ )."/actions.php";
        $chain->next($context);
    }
}
class com_yizin_fm_PrivilegeExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['items']['fm']['text'] = "插件管理";
        $context['items']['fm']['items']['fm']['text'] = "插件管理";
        $chain->next($context);
    }
}
class com_yizin_fm_LoginInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $chain->next($context);
        } else {
            $mvc = new MVC(array());
            $mvc->setMessage("message", "请先登录");
            $currentURI = Utils::getURI();
            $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
        }
    }
}
class com_yizin_fm_AdminInterceptor extends Interceptor {
    public function intercept(&$context, InterceptorChain $chain) {
        
        global $mvc,$featureManager;
        global $currentUser;
        $currentUser = session_getCurrentUser();
        if (isset($currentUser)) {
            $userContext = array("user"=>$currentUser['serial']);
            $featureManager->apply("com.yizin.platform.role.get.by.user", $userContext);
            $roleContext = array('role'=>$userContext['role']['serial'], 'id'=>'fm.fm');
            $featureManager->apply("com.yizin.platform.privilege.get.by.id", $roleContext);
            if (!($roleContext['allow'])) {
        
                $mvc = new MVC(array());
                $mvc->setMessage("message", "帐户权限不足");
                $currentURI = Utils::getURI();
                $mvc->redirect(REQUEST_BASE.'login'.$currentURI);
            };
        }
        $chain->next($context);
    }
}
class com_yizin_fm_InterceptorExtension extends Extension {
    public function apply(&$context, Chain $chain) {
        $context['rule']['{^/admin/fm/.*$}'][] = new com_yizin_fm_LoginInterceptor();
        $context['rule']['{^/admin/fm/.*$}'][] = new com_yizin_fm_AdminInterceptor();
        $chain->next($context);
    }
}
class com_yizin_fm_FMFeature extends Feature
{
    public function registerExtension()
    {
        $this->extensions = Array(
        new com_yizin_fm_RouterExtension("com.yizin.platform.router"),
		new com_yizin_fm_InterceptorExtension("com.yizin.platform.interceptor"), 
		new com_yizin_fm_PrivilegeExtension("com.yizin.platform.privilege"),
        );
    }

    public function registerExtendable()
    {
    }

    public function init()
    {
    }

    public function destroy()
    {
    }
}

$featureClass = "com_yizin_fm_FMFeature";
$featureName = "FM Feature";
$featureIntroduction = "FM feature";
?>
